package org.zuel.huhuForum.service.Impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.zuel.huhuForum.dao.dc.ReadedMapper;
import org.zuel.huhuForum.dao.um.HistorySeachMapper;
import org.zuel.huhuForum.model.*;
import org.zuel.huhuForum.service.IDiscussPostService;

import java.util.*;

@Service
public class HistoryService {
    @Autowired
    private HistorySeachMapper historySeachMapper;

    @Autowired
    private ReadedMapper readedMapper;

    @Autowired
    private IDiscussPostService discussPostService;

    public void addHistory(HistorySeach historySeach){
        historySeachMapper.insert(historySeach);
    }

    public void addReaded(Readed readed){
        readedMapper.insert(readed);
    }

    public List<String> getHistoryAndRead(Integer userid){
        List<Readed> readeds=readedMapper.selectByUserID(userid);
        Set<String> set=new HashSet<>();
        int i,j=0;
        Random rand = new Random();
        while(j<readeds.size()/2){
            if(readeds.size()==0){
                break;
            }
            i=rand.nextInt(readeds.size());
            if(!readeds.get(i).getTheme().isEmpty()){
                set.add(readeds.get(i).getTheme());
            }
            j++;
        }
        HistorySeachExample historySeachExample=new HistorySeachExample();
        HistorySeachExample.Criteria criteria1=historySeachExample.createCriteria();
        criteria1.andUserIdEqualTo(userid);
        List<HistorySeach>historySeaches= historySeachMapper.selectByExample(historySeachExample);
        j=0;
        while(j<historySeaches.size()/2){
            if(historySeaches.size()==0){
                break;
            }
            i=rand.nextInt(historySeaches.size());
            set.add(historySeaches.get(i).getContent());
            j++;
        }
        List<String>list=new ArrayList<>(set);
        return list;
    }

}
